package com.caishi.lkx.common;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.zzw.common.diamond.Diamond;
import lombok.Getter;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author huangpeng
 * @Description //敏感词
 * @Date 2022/9/14 16:37
 **/
@Configuration
public class Sensitive {

    public static Sensitive owner;

    public Sensitive() {
        owner = this;
    }

    @Getter
    @Diamond("SENSITIVE")
    private List<String> sensitive;


    public static String replace(String str) {
        if (StrUtil.isBlank(str)) return str;
        var sensitive = owner.getSensitive();
        if (null == sensitive) return str;
        for (var o : sensitive) {
            str = str.replaceAll(o, "*".repeat(o.length()));
        }
        return str;
    }
    public static Boolean isSensitive(String str){
        var sensitive = owner.getSensitive();
        return sensitive.contains(str);
    }

    public void setSensitive(String sensitive) {
        if (StrUtil.isEmpty(sensitive)) return;
        this.sensitive = JSONArray.parseArray(sensitive).toJavaList(String.class);
    }
}
